import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/AppData.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

//收藏控制器
class CollectController extends BaseController{

  String actionTxt = '编辑';
  
  bool showButton = false;

  int page = 1;

  ///是否全选
  bool selectedAll = false;

  ///选中的数量
  int choiseNum = 0;

  bool hasNextPage = false;


  //选中的商品id数组
  List<String> goodsIdList = [];

  List data = [];

  List keys = [];


  @override
  void onInit() {
    getCollectList();
    super.onInit();
  }

  //收藏列表
  void getCollectList() async{
    Tools.showLoading();
     await Network.getCollectList({
      "pageNum":page,
      "pageSize":10
     }).then((value) {
      if(page>1){
        data.addAll(value['list']);
      }else{
        data = value['list'];
      }
      
      hasNextPage = value['hasNextPage'];


      if(data.isNotEmpty){
        for(int i=0;i<data.length;i++){
          data[i]['isSelect'] = 0;
        }
      }
      print(data);
      
      update();
       
      Tools.hideLoading();
    }).catchError((error){
      print(error);
      Tools.hideLoading();
    });
  }

  ///全选反选
  void selectedAllItem(bool select){
    if(data.isNotEmpty){
      selectedAll = select;
      int isSelect;
      if(select){
        isSelect = 1;
      }else{
        isSelect = 0;
      }

      if(data.isNotEmpty){
        for(int i=0;i<data.length;i++){
          data[i]['isSelect'] = isSelect;
        }
      }

      choiseNum = getNowChoiseGoods().length;

      update();
    }else{
      Tools.showToast("没东西可选");
    }
  }


  //选择
  void choise(String id, bool select){
    if(!select&&selectedAll){
      //自动取消全选
      selectedAll = false;
    }

    if(data.isNotEmpty){
        for(int i=0;i<data.length;i++){
          if(data[i]["commodityId"]==id){
            data[i]['isSelect'] = select==true?1:0;
          }
        }
        choiseNum = getNowChoiseGoods().length;
        if(choiseNum==data.length){
          selectedAll = true;
        }
        update();
      }
      
  }



  //编辑控制
  void edit(){
    if(!showButton){
      showButton = true;
      actionTxt = '完成';
    }else{
      showButton = false;
      actionTxt = '编辑';
    }
    update();
  }


  //加入购物车
  void addCar(String goodsId){
    Tools.showModal(
        content: "要将该商品加入购物车么?",
        onConfirm: (tag) {
          Network.addCar({
            "buyNum":1,
            "goodsId":goodsId,
            "isSelect":1,
            "isSend":0,
            "sid":AppData.sid,
            "specNum":1
          }).then((value){
            if(value){
              Tools.showToast("已加入购物车");
              SmartDialog.dismiss(tag: tag);
            }
          });
        },
        onCancel: (tag) {
          SmartDialog.dismiss(tag: tag);
        },
      );
  }


  //批量取消收藏
  void delete(){
    List ids = getNowChoiseGoods();
    if(ids.isEmpty){
      Tools.showToast("请选择要取消的记录");
    }else{
      Tools.showModal(
        content: "确定要取消收藏吗?",
        onConfirm: (tag) {
          Network.deleteCollectMut(ids).then((value){
            if(value){
              Tools.showToast("操作完成");
              showButton = false;
              actionTxt = '编辑';
              choiseNum = 0;
              SmartDialog.dismiss(tag: tag);
              getCollectList();
              update();
            }
          });
        },
        onCancel: (tag) => {SmartDialog.dismiss(tag: tag)},
      );
    }
  }

  //删除单个收藏
  void deleteOne(String goodsId,int index){
    Network.deleteCollect(goodsId).then((value){
      Tools.showLoading();
      if(value){
        Tools.hideLoading();
        data.remove(data[index]);
        update();
        Tools.showToast("取消成功");
      }
    }).onError((error, stackTrace){
      Tools.hideLoading();
    });
  }


  //获取当前选中的商品
  List getNowChoiseGoods(){
    List<String> ids = [];
    if(data.isNotEmpty){
      for(int i =0;i<data.length;i++){
         if(data[i]['isSelect']==1){
          ids.add(data[i]['commodityId']);
         }
      }
    }
    return ids;
  }


  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    selectedAll = false;//刷新取消全选
    // page = 1;
    getCollectList();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading()async {
    if(hasNextPage==true){
      super.onPageLoading();
      page += 1;
      getCollectList();
      baseRefreshCtr.loadComplete();
    }
  }

  @override
  void onClose() {

    super.onClose();
  }
}